package com.example.demo05datajps.controller;

import com.example.demo05datajps.entity.User;
import com.example.demo05datajps.repository.UserRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class UserController {
    private final UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping(path = "/user/{id}")
    public User getUser(@PathVariable Integer id) {
        Optional<User> byId = userRepository.findById(id);
        User user = byId.get();
        return user;
    }

    @RequestMapping(path = "/user")
    public User insertUser(User user) {
        User save = userRepository.save(user);
        return save;
    }
}
